/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.starter.web.framework.flow;

import cn.hallele.ms.starter.web.framework.context.ActionContext;
import cn.hallele.ms.starter.web.framework.node.ServiceFlow;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.flow.LiteflowResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.Future;

/**
 * hallele_mc_core
 *
 * @author anle5
 * @since 2024/12/23 23:33
 */
@Slf4j
@LiteflowComponent
public class ChainFlow extends ServiceFlow {
    @Autowired(required = false)
    private FlowExecutor flowExecutor;

    @Override
    public void process() throws Exception {
        ActionContext context = this.getContextBean(ActionContext.class);
        LiteflowResponse response;
        if (context.isSync()) {
            response =
                    flowExecutor.execute2RespWithRid(context.getChainId(), this.getRequestData(), this.getSlot().getRequestId(), context);
        } else {
            Future<LiteflowResponse> future =
                    flowExecutor.execute2FutureWithRid(context.getChainId(), this.getRequestData(), this.getSlot().getRequestId(), context);
            response = future.get();
        }

        context.setChainResponse(response);
    }
}
